##
# Main simulation window
##

from WtfEngine import *;
import WtfGui;

gFontData = WtfGui.FontData("media/fonts/sans_black", 12);
DataManager.GetInst().Insert(gFontData);
DataManager.GetInst().Insert(WtfGui.FontSetting("v_wtfEdit_ui_font", "media/fonts/sans_black"));

import ObjectView, DataView;

font = WtfGui.Font("v_wtfEdit_ui_font");

#
# The actual interface. This also encapsulates the game state (by the window manager)
#
class Interface(WtfGui.ScreenOverlay):
	def __init__(self):
		
		WtfGui.ScreenOverlay.__init__(self);
		
		ctrOpts = WtfGui.LayoutContainerCreationOptions();

		opts = StaticCreationOptions();
		opts.name = "object0";
		s = Static(opts, Oblong(100.0, 1.0, 100.0), OblongModel("", Vector(100,1,100)));
		s.position = Vector(0, -10, 0);
		
		camOpts = PannableCameraOptions();
		self.camera = PannableCamera(camOpts, Vector(0, 100, 100), Vector(0, -100, -100));
		Renderer.GetInst().camera = self.camera;
		self.camera.BindToKeyboardAndMouse();
		
		InputHandler.GetInst().EnterGuiMode();
		
		# Create bars
		self.topBar = TopBar(ctrOpts, self);
		self.leftBar = LeftBar(self);
		self.rightBar = RightBar(self);

		# Add elements
		self.AddChild(WtfGui.LayoutPositioner(self.topBar, WtfGui.CentrePositioner(), WtfGui.TopPositioner()));
		self.AddChild(WtfGui.LayoutPositioner(self.leftBar, WtfGui.LeftPositioner(), WtfGui.CentrePositioner()));
		self.AddChild(WtfGui.LayoutPositioner(self.rightBar, WtfGui.RightPositioner(), WtfGui.CentrePositioner()));


class LeftBar(WtfGui.LayoutContainer):
	def __init__(self, iface):
		ctrOpts = WtfGui.LayoutHiderCreationOptions();
		WtfGui.LayoutContainer.__init__(self, ctrOpts, 1);
		
		self.paddingLT = iVector2D(3, 0);
		self.paddingRB = iVector2D(3, 3);
		
		self.AddChild(ObjectView.ObjectView());

class RightBar(WtfGui.LayoutContainer):
	def __init__(self, iface):
		ctrOpts = WtfGui.LayoutHiderCreationOptions();
		WtfGui.LayoutContainer.__init__(self, ctrOpts, 1);
		
		self.paddingLT = iVector2D(3, 0);
		self.paddingRB = iVector2D(3, 3);
		
		self.AddChild(DataView.DataView());
		
		
class DataMenu(WtfGui.Menu):
	def __init__(self, iface):
		WtfGui.Menu.__init__(self, "Data Menu");
		
		self.size = iVector2D(200, 0);
		
		self.btnDataMgmt = WtfGui.Button(Function(lambda: iface.AddChild(DataManagement())), "Manage Data", font);
		self.btnDataMgmt.ExpandToText();
		
		self.AddChild(self.btnDataMgmt);

class EngineMenu(WtfGui.Menu):
	def __init__(self, iface):
		WtfGui.Menu.__init__(self, "Data Menu");
		
		self.size = iVector2D(200, 0);
		
		self.btnConsole = WtfGui.Button(Function(lambda: iface.SpawnConsole()), "Console", font);
		self.btnConsole.ExpandToText();
		
		self.AddChild(self.btnConsole);
		
class DataManagement(WtfGui.Window):
	def __init__(self):
		WtfGui.Window.__init__(self, "Data Management");
		
		self.font = WtfGui.Font("v_gui_font");
		
		self.table = WtfGui.Table(WtfGui.ScrollLayoutCreationOptions(), 1);
		self.table.AddHeading(WtfGui.TableHeading(self.innerSize.x / 2, WtfGui.Label("Name", self.font)));
		self.table.AddHeading(WtfGui.TableHeading(self.innerSize.x / 2, WtfGui.Label("Type", self.font)));
		
		map(lambda obj: self.AddObject(obj.data().ref), DataManager.GetInst().objects);
		
		self.table.size = self.innerSize;
		self.AddChild(self.table);
		
	def AddObject(self, obj):
		rec = self.table.AddRecord();
		rec.AddChild(WtfGui.Label(obj.name, self.font));
		rec.AddChild(WtfGui.Label(type(obj).__name__, self.font));
		
		
class TopBar(WtfGui.LayoutContainer):
	def __init__(self, ctrOpts, iface):
		WtfGui.LayoutContainer.__init__(self, ctrOpts, 0);
		
		self.paddingLT = iVector2D(3, 0);
		self.paddingRB = iVector2D(3, 3);
		
		font = WtfGui.Font("v_gui_font");
		self.simToggle = WtfGui.Button(Function(self.togglePause), "Pause", font);
		self.mnuEngine = WtfGui.Button(Function(lambda: iface.AddChild(WtfGui.PopupWidget(EngineMenu(iface))) ), "Engine", font);
		self.mnuData = WtfGui.Button(Function(lambda: iface.AddChild(WtfGui.PopupWidget(DataMenu(iface))) ), "Data", font);
		
		self.simToggle.ExpandToText();
		self.mnuEngine.ExpandToText();
		self.mnuData.ExpandToText();
		
		self.AddChild(self.simToggle);
		self.AddChild(self.mnuEngine);
		self.AddChild(self.mnuData);
	
	def togglePause(self):
		s = Kernel.GetInst().GetGameState();
		if s.timeScale == 0:
			s.timeScale = 1;
			self.simToggle.caption = "Pause";
			self.simToggle.ExpandToText();
		else:
			s.timeScale = 0;
			self.simToggle.caption = "Run";
			self.simToggle.ExpandToText();

class GameMenu(WtfGui.WindowManager):
	def __init__(self):
		
		WtfGui.WindowManager.__init__(self);
		
		ctrOpts = WtfGui.LayoutContainerCreationOptions();
		
		# Create menu
		font = WtfGui.Font("v_gui_font");

		self.menu = WtfGui.Menu("Game Menu");
		self.menu.Resize(300, 100);

		self.menu.AddChild(WtfGui.Button(Function
			(lambda: self.WmToggle()), "Resume Game", font));
		self.menu.AddChild(WtfGui.Button(Function
			(lambda: gWm.AddWindow(Options(font), 1)), "Options", font));
		self.menu.AddChild(WtfGui.Button(Function
			(lambda: self.SpawnConsole()), "Developer's Console", font));
		self.menu.AddChild(WtfGui.Button(Function
			(lambda: self.AddWindow(WtfGui.SceneDiagnostics(), 1)), "Diagnostics", font));
		self.menu.AddChild(WtfGui.Button(Function(lambda: exit()), "Exit", font));
		
		# Add elements
		self.AddChild(WtfGui.LayoutPositioner(self.menu, WtfGui.CentrePositioner(), WtfGui.CentrePositioner()));
		